<?php

namespace common\modules\user\models\backend;

use common\widgets\ActiveForm;
use common\widgets\Form;
use common\widgets\Select2;
use Yii;
use yii\helpers\ArrayHelper;

class PermissionForm extends Permission
{

    /**
     * @inheritdoc
     */
    public $isNewRecord;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return parent::rules();
    }

    const SCENARIO_CREATE = 'admin-create';
    const SCENARIO_UPDATE = 'admin-update';

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        return [
            'admin-create' => array_keys($this->attributeLabels()),
            'admin-update' => array_keys($this->attributeLabels()),
        ];
    }

    /**
     * 返回当前表单的名称
     *
     * @return string
     */
    public function classLabelName()
    {
        return Yii::t('backend/user', 'Permission Form');
    }

    /**
     * 列表定义
     *
     * @param ActiveForm|Form $form
     * @param static          $model ItemForm
     * @param mixed           $extra
     * @return array
     * @throws \yii\web\ServerErrorHttpException
     */
    public function formRows(&$form, $model, $extra)
    {
        return [
            [
                'columns'    => 2,
                'attributes' => [
                    'name' => [
                        'type'    => Form::INPUT_TEXT,
                    ],

                    'ruleName' => [
                        'type'        => Form::INPUT_WIDGET,
                        'widgetClass' => Select2::className(),
                        'options'     => [
                            'data' => ArrayHelper::map($model->rules, 'name', 'name'),
                            'options'       => [
                                'prompt' => Yii::t('backend/user', 'BACKEND_PERMISSIONS_RULE_NAME_PROMPT'),
                            ],
                            'pluginOptions' => [
                                'width' => '100%',
                            ],
                        ],
                    ],
                ],
            ],
            [
                'columns'    => 2,
                'attributes' => [
                    'description' => [
                        'type'    => Form::INPUT_TEXTAREA,
                    ],
                    'data' => [
                        'type'    => Form::INPUT_TEXTAREA,
                    ],
                ],
            ],
            [
                'columns'    => 1,
                'attributes' => [
                    'children' => [
                        'type'        => Form::INPUT_WIDGET,
                        'widgetClass' => Select2::className(),
                        'options'     => [
                            'options'       => [
                                'multiple'    => true,
                                'placeholder' => Yii::t('backend/user', 'BACKEND_PERMISSIONS_CHILDREN_PROMPT'),
                            ],
                            'pluginOptions' => [
                                'width' => '100%',
                            ],
                            'data' => ArrayHelper::map($model->permissions, 'name', 'name'),
                        ],
                    ],
                ],
            ],
        ];
    }
}
